import 'package:fate_blind_box/chat_view/ahu_text.dart';
import 'package:fate_blind_box/config/app_style.dart';
import 'package:fate_blind_box/model/chat_friend_message_entity.dart';
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';

class TextMessageView extends StatelessWidget {
  final EMMessage message;
  final EMTextMessageBody messageBody;
  final ChatFriendMessageEntity? model;
  final EMUserInfo? userInfo;

  const TextMessageView({
    super.key,
    required this.message,
    required this.messageBody,
    this.model,
    this.userInfo,
  });

  @override
  Widget build(BuildContext context) {
    bool isNotSelf = message.direction == MessageDirection.RECEIVE;
    Color bgColor = isNotSelf ? Colors.white : ThemeColor.colorCF8F23;
    Color textColor = isNotSelf ? Colors.black : Colors.white;
    if (message.chatType == ChatType.Chat) {
      return GestureDetector(
        onLongPress: () {
          Clipboard.setData(ClipboardData(text: messageBody.content));
          EasyLoading.showToast('复制成功');
        },
        child: Container(
          padding: const EdgeInsets.all(10),
          margin: const EdgeInsets.only(top: 10, bottom: 10),
          constraints: const BoxConstraints(maxWidth: 240),
          //height: 40,
          decoration: BoxDecoration(
              color: bgColor,
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(isNotSelf ? 0 : 16.w),
                topRight: Radius.circular(isNotSelf ? 16.w : 0),
                bottomLeft: Radius.circular(16.w),
                bottomRight: Radius.circular(16.w),
              )),
          child: Text(
            messageBody.content,
            style: TextStyle(
              color: textColor,
              fontSize: 14,
            ),
          ),
        ),
      );
    }
    return groupWidget();
  }

  ///
  Widget groupWidget() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        // message.direction == MessageDirection.RECEIVE
        //     ? personWidget()
        //     : const SizedBox(),
        SizedBox(
            height: message.direction == MessageDirection.RECEIVE ? 6.w : 0),
        Container(
          padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 8.w),
          constraints: const BoxConstraints(maxWidth: 240),
          decoration: BoxDecoration(
              color: message.direction == MessageDirection.RECEIVE
                  ? Colors.white
                  : ThemeColor.colorCF8F23,
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(
                      message.direction == MessageDirection.RECEIVE
                          ? 0.w
                          : 16.w),
                  topRight: Radius.circular(
                      message.direction == MessageDirection.SEND ? 0.w : 16.w),
                  bottomLeft: Radius.circular(16.w),
                  bottomRight: Radius.circular(16.w))),
          child: Text(
            messageBody.content,
            style: TextStyle(
              color: message.direction == MessageDirection.RECEIVE
                  ? Colors.black
                  : Colors.white,
              fontSize: 14,
            ),
          ),
        ),
      ],
    );
  }

  /// 成员信息
  Widget personWidget() {
    if (userInfo?.gender == 2) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Container(
            padding: EdgeInsets.symmetric(vertical: 2.w),
            margin: EdgeInsets.only(right: 4.w),
            decoration: BoxDecoration(
              color: '#FFF3FD'.stringToColor,
              borderRadius: BorderRadius.circular(8.w),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(width: 6.w),
                Image.asset(
                  'images/user/txl_woman_icon.png',
                  width: 12.w,
                  height: 12.w,
                ),
                SizedBox(width: 3.w),
                AhuText(
                  text: '${userInfo?.birth}',
                  fontSize: 12,
                  color: '#FE73CF'.stringToColor,
                  fontWeight: FontWeight.w400,
                ),
                SizedBox(width: 6.w),
              ],
            ),
          ),
          AhuText(
            text: '${userInfo?.nickName}',
            color: '#868686'.stringToColor,
            fontSize: 11,
            fontWeight: FontWeight.w400,
          )
        ],
      );
    }
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Container(
          padding: EdgeInsets.symmetric(vertical: 2.w),
          margin: EdgeInsets.only(right: 4.w),
          decoration: BoxDecoration(
            color: '#F3F7FF'.stringToColor,
            borderRadius: BorderRadius.circular(8.w),
          ),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              SizedBox(width: 6.w),
              Image.asset(
                'images/user/txl_man_icon.png',
                width: 12.w,
                height: 12.w,
              ),
              SizedBox(width: 3.w),
              AhuText(
                text: '${userInfo?.birth}',
                fontSize: 12,
                color: '#739BFE'.stringToColor,
                fontWeight: FontWeight.w400,
              ),
              SizedBox(width: 6.w),
            ],
          ),
        ),
        AhuText(
          text: '${userInfo?.nickName}',
          color: '#868686'.stringToColor,
          fontSize: 11,
          fontWeight: FontWeight.w400,
        )
      ],
    );
  }
}
